package com.example.stlink.configs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.mapapi.search.core.PoiInfo;
import com.example.stlink.R;

import java.util.List;

/**
 * 用于将课程数据渲染进ListView控件中进行显示
 */
public class LocationPoiAdapter extends RecyclerView.Adapter<LocationPoiAdapter.ViewHolder> implements View.OnClickListener  {

    private com.example.stlink.configs.onRecyclerViewItemClickListener  onRecyclerViewItemClickListener;
    private RecyclerView rvParent;


    private List<PoiInfo> mItems;
    private final Context mContext;
//    private LayoutInflater mInflater;


    public LocationPoiAdapter(@NonNull Context mContext, @NonNull List<PoiInfo> mItems) {
        this.mItems = mItems;
        this.mContext = mContext;
//        this.mInflater=LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //指定一个父控件
        rvParent = (RecyclerView) parent;
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.card_location_poi, parent, false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.tvBuildingName.setText(mItems.get(position).name);
        holder.tvBuildingDescription.setText(mItems.get(position).address);
        System.out.println("LocationPoiAdapter : holder : " + holder);
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    //重载点击事件，指定点击事件时执行自定义的onItemClick
    @Override
    public void onClick(View view) {
        int position = rvParent.getChildAdapterPosition(view);
        if (onRecyclerViewItemClickListener != null){
            onRecyclerViewItemClickListener.onItemClick(rvParent, view, position);
        }
    }

    //实例化自定义接口
    public void setOnItemClickListener(com.example.stlink.configs.onRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvBuildingName;
        TextView tvBuildingDescription;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvBuildingName = itemView.findViewById(R.id.tv_building_name);
            tvBuildingDescription = itemView.findViewById(R.id.tv_building_description);
        }

        @NonNull
        @Override
        public String toString() {
            return "ViewHolder{" +
                    "tvBuildingName=" + tvBuildingName +
                    ", tvBuildingDescription=" + tvBuildingDescription +
                    '}';
        }
    }

}
